<template>
	<view class="box">
		<!-- logo -->
		<view class="login_title">
			欢迎登录小U商城
		</view>
		<image class="login_logo" src="../../static/images/logo1.png" alt="" />
		<button class="login_but" hover-class="touchOpacity" open-type="getPhoneNumber" @getphonenumber="wxLogin">
			微信用户一键登录
		</button>
		<view class="login_text" hover-class="touchOpacity" @click="goTextLogin">手机号验证注册/登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {},
		methods: {
			// 跳转到手机号注册/登录页面
			goTextLogin() {
				uni.navigateTo({
					url: '/pages/textlogin/textlogin'
				})
			},
			// 微信一键登录
			wxLogin(e){
				console.log(e);
				// #ifndef MP-WEIXIN
				uni.showToast({
					title:'此平台不支持微信一键登录',
					icon:'none'
				})
				// #endif
				
				// #ifdef MP-WEIXIN
				if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){
					uni.showToast({
						title:'您拒绝了登录',
						icon:'error'
					})
					return
				}
				let encryptedData = e.detail.encryptedData
				let iv = e.detail.iv
				// console.log(encryptedData,iv);
				wx.login({
					success:async (res) => {
						let code = res.code
						console.log(encryptedData);
						console.log(iv);
						console.log(code);
						// return
						let option = {
							url:'/api/wxlogin',
							method:'POST',
							data:{
								encryptedData,
								iv,
								code
							}
						}
						let result = await this.$http(option)
						console.log(result);
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				})
				// #endif
			}
		}
	}
</script>

<style>
	/* 导入外部的样式文件 */
	@import url("@/static/css/login.css");
</style>